﻿using System.Collections.Generic;
using System.Linq;
using MRGSP.SurveyMaster.Core;
using MRGSP.SurveyMaster.Core.Model;
using MRGSP.SurveyMaster.Core.Service.Utils;

namespace MRGSP.SurveyMaster.Service.Utils
{
    public class QuestionManager : IQuestionManager
    {
        public IEnumerable<Question> ColorizeErrors(IEnumerable<Question> source, IEnumerable<ValidationMessage> messages)
        {
            foreach (var question in messages.Select(message => source.Single(o => o.Id == message.QuestionId)))
            {
                question.Red = true;
                foreach (var child in source.Where(o => o.ParentId == question.Id))
                {
                    child.Green = true;
                }
            }

            return source;
        }

        public IEnumerable<Question> RebuildSurveyQuestions(IEnumerable<Question> source, IEnumerable<Answer> answers)
        {
            foreach (var answer in answers)
            {
                var q = source.Where(o => o.Id == answer.QuestionId).Single();
                if (q.Type == ControlType.Textbox || q.Type == ControlType.TextArea)
                    q.Value = answer.Value;
                else
                {
                    q.Value = true;
                }
            }
            return source;
        }
    }
}
